package com.javaee.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.javaee.ejb.entity.Employee;
import com.javaee.ejb.entity.Project;
import com.javaee.ejb.session.remote.EmployeeManagerRemote;
import com.javaee.ejb.session.remote.ProjectManagerRemote;
import com.javaee.web.HttpServletBase;
import com.javaee.web.SiteParameters;

/**
 * Servlet implementation class AddProjectServlet
 */
public class AddProjectServlet extends HttpServletBase {
	private static final long serialVersionUID = 1L;

	@EJB
	private ProjectManagerRemote projectManager;

	@EJB
	private EmployeeManagerRemote employeeManager;

	/**
	 * @see HttpServletBase#HttpServletBase()
	 */
	public AddProjectServlet() {
		super();
	}

	@Override
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String projectName = null;
		int budget = 0;
		int managerId = 0;
		String description = null;

		// Verification des valeurs transmises
		HashMap<String, Boolean> hashInvalid = new HashMap<String, Boolean>();
		HashMap<String, String> hashValue = new HashMap<String, String>();

		hashValue.put("projectName", request.getParameter("projectName"));
		hashValue.put("budget", request.getParameter("budget"));
		hashValue.put("managerId", request.getParameter("managerId"));
		hashValue.put("description", request.getParameter("description"));

		projectName = request.getParameter("projectName");

		if (projectManager.getByName(projectName) != null) {
			hashInvalid.put("projectName", new Boolean(false));
		}

		try {
			budget = Integer.parseInt(request.getParameter("budget"));
		} catch (Exception e) {
			hashInvalid.put("budget", new Boolean(false));
		}

		try {
			managerId = Integer.parseInt(request.getParameter("managerId"));
		} catch (Exception e) {
			hashInvalid.put("managerId", new Boolean(false));
		}

		if (hashInvalid.isEmpty() == false) {
			// Le formulaire est invalide
			request.setAttribute("hashInvalid", hashInvalid);
			request.setAttribute("hashValue", hashValue);
			request.setAttribute("formInvalid", "projectForm");

			loadJSP(SiteParameters.getServletPath("administration"), request,
					response);
			return;
		} else {
			description = request.getParameter("description");
			Employee manager = employeeManager.getById(managerId);
			List<Employee> members = new ArrayList<Employee>();
			members.add(manager);

			Project project = new Project();

			project.setName(projectName);
			project.setBudget(budget);
			project.setManager(manager);
			project.setDescription(description);
			project.setMembers(members);

			// Sauvegarde de l'employe
			projectManager.saveProject(project);

			// Redirection vers la page d'administration
			response.sendRedirect(SiteParameters
					.getFullServletPath("administration"));
		}
	}
}
